package com.shenzhen.horselive.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.flqy.baselibrary.utils.ScreenUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.shenzhen.horselive.MainActivity;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.base.BaseActivity;

public class WellcomActivity extends BaseActivity {


    private Handler  handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //这里开始加载广告
            AdSlot adSlot = new AdSlot.Builder()
                    // 必选参数 设置您的CodeId
                    .setCodeId("887390640")
                            // 必选参数 设置广告图片的最大尺寸及期望的图片宽高比，单位Px
                            // 注：必填字段，期望的图片尺寸，返回尺寸可能有差异
                            .setImageAcceptedSize(ScreenUtil.getScreenWidth(), ScreenUtil.getScreenHeight())
                            //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
//                            .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight)
                            // 可选参数，针对信息流广告设置每次请求的广告返回个数，最多支持3个
//                            .setAdCount(3)
//                            //请求原生广告时候需要设置，参数为TYPE_BANNER或TYPE_INTERACTION_AD
//                            .setNativeAdType(AdSlot.TYPE_SPLASH)
                            .build();
            TTAdSdk.getAdManager().createAdNative(getContext()).loadSplashAd(adSlot,
                    new TTAdNative.SplashAdListener() {
                @Override
                public void onError(int i, String s) {
                    gotoMain();
                }

                @Override
                public void onTimeout() {
                    gotoMain();
                }

                @Override
                public void onSplashAdLoad(TTSplashAd ttSplashAd) {

                     linearLayout.removeAllViews();
                     linearLayout.addView(ttSplashAd.getSplashView());
                     ttSplashAd.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                         @Override
                         public void onAdClicked(View view, int i) {

                         }

                         @Override
                         public void onAdShow(View view, int i) {

                         }

                         @Override
                         public void onAdSkip() {
                              gotoMain();
                         }

                         @Override
                         public void onAdTimeOver() {
                              gotoMain();
                         }
                     });
                }
            },5000);
        }
    }
            ;

    //跳转主页面
    private void gotoMain() {
        if (User.get().isLogin())
        {
            startActivity(new Intent(WellcomActivity.this, MainActivity.class));
            finish();
        }else
        {
            startActivity(new Intent(WellcomActivity.this,LoginActivity.class));
            finish();
        }
    }

    ImageView  imageViewA;
    LinearLayout  linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wellcom);
        //广告需要的权限
        linearLayout = findViewById(R.id.container);
        imageViewA = findViewById(R.id.img_a);
        Animation animation = new AlphaAnimation(1.0f, 0.0f);
        animation.setDuration(1500);
        imageViewA.startAnimation(animation);
//        imageViewA.setAnimation();
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                imageViewA.setVisibility(View.GONE);
                //结束
                handler.sendMessageDelayed(new Message(),1000);
            }
            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }
}